package cn.tedu.csmallproductzxj.service.impl;

import cn.tedu.csmallproductzxj.ex.ServiceException;
import cn.tedu.csmallproductzxj.mapper.AlbumMapper;
import cn.tedu.csmallproductzxj.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmallproductzxj.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmallproductzxj.pojo.entity.Album;
import cn.tedu.csmallproductzxj.pojo.vo.AlbumListItemVO;
import cn.tedu.csmallproductzxj.pojo.vo.AlbumStandardVO;
import cn.tedu.csmallproductzxj.service.IAlbumService;
import cn.tedu.csmallproductzxj.web.JsonResult;
import cn.tedu.csmallproductzxj.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlbumServiceImpl implements IAlbumService {
    @Autowired
    private AlbumMapper albumMapper;


    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO){
        // 从参数对象中获取相册名称
        String name= albumAddNewDTO.getName();
        // 调用albumMapper的int countByName(String name)根据名称执行统计
        int countByName=albumMapper.countByName(name);
        // 判断统计结果是否大于0
        if(countByName>0){
            // 是：抛出异常throw new RuntimeException()
            String message = "添加相册失败，相册名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        // 创建Album对象
        Album album=new Album();
        // 调用BeanUtils.copyProperties()将参数对象的属性值复制到Album对象中
        BeanUtils.copyProperties(albumAddNewDTO,album);
        // 调用albumMapper的int insert(Album album)方法将相册数据插入到数据库
        albumMapper.insert(album);
    }

    @Override
    public void delete(Long id) {
        AlbumStandardVO queryResult=albumMapper.getStandardById(id);
        if(queryResult == null){
            String message="获取相册详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        albumMapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, AlbumUpdateDTO albumUpdateDTO) {
        AlbumStandardVO queryResult=albumMapper.getStandardById(id);
        if(queryResult == null){
            String message="修改相册详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //修改的name 不允许出现其他相册的名称，自己的可以，（相当于不修改name)
        String name= albumUpdateDTO.getName();
        int count=albumMapper.countByNameAndNotId(id,name);
        if (count > 0) {
            String message = "修改相册详情失败，相册名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Album album=new Album();
        BeanUtils.copyProperties(albumUpdateDTO,album);
        album.setId(id);
        albumMapper.update(album);
    }

    @Override
    public AlbumStandardVO getStandardById(Long id) {
        AlbumStandardVO queryResult=albumMapper.getStandardById(id);
        if(queryResult == null){
            String message="获取相册详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return queryResult;
    }

    @Override
    public int countByName(String username) {
        int num= albumMapper.countByName(username);
        if(num==0){
            String message="数量为0";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return num;
    }

    @Override
    public int countByNameAndNotId(Long id,String name){
        int num=albumMapper.countByNameAndNotId(id,name);
        if(num==0){
            String message="数量为0";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return num;
    }

    @Override
    public List<AlbumListItemVO> list() {
        List<AlbumListItemVO> list=albumMapper.list();
        return list;
    }
}
